//
//  XLVistor.swift
//  XLVeibo
//
//  Created by macbook on 16/6/25.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit
import SnapKit

class XLVistor: UIView {

    //按钮的闭包，用来跳转控制器
    var close : (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //添加控件
    private func setUI(){
        
        backgroundColor = RBG(237, green: 237, blue: 237)
        //添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(contentLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //添加约束
        feedImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        houseImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(16) 
        }
        
        registerBtn.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.right.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(16)
        }
    
    
    }
    
    
    
    
    //懒加载控件
    //圆圈
    private lazy var feedImageView: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩 这个就用自定义的便利构造函数
    private lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    //房子
    private lazy var houseImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    //文字
    private lazy var contentLabel: UILabel = {
    
        let contencLabel = UILabel()
        contencLabel.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        contencLabel.numberOfLines = 0
        contencLabel.font = UIFont.systemFontOfSize(14)
        contencLabel.textAlignment = NSTextAlignment.Center
        
        return contencLabel
    }()
    
    
    //登陆
    lazy var loginBtn: UIButton = {
    
        let loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //添加方法
        loginBtn.addTarget(self, action: #selector(XLVistor.loginBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
    }()
    
    
    //注册
    lazy var registerBtn: UIButton = {
    
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //添加方法
        btn.addTarget(self, action: #selector(XLVistor.loginBtnClick), forControlEvents: UIControlEvents.TouchUpInside)

      return btn
    }()
    
    // MARK: - 登陆
    func loginBtnClick(){
    
      close?()
    
    }
    
   
    
    // MARK: - 旋转
    func setAnimate(){
    
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画时间
        animate.duration = 15
        //设置角度
        animate.toValue = M_PI * 2
        //设置圈数
        animate.repeatCount = MAXFLOAT
        //当app推送到后台或者切换控制器的实际，默认动画完成
        animate.removedOnCompletion = false
        
        //添加动画
        feedImageView.layer.addAnimation(animate, forKey: nil)
    }
    
    
    //外界调用的方法，设置访客视图
    func setupVistorInfor(imageName:String?,title: String?){
    
        //如果有值就代表不是主页
        if let image = imageName,let tit = title{
            houseImageView.image = UIImage(named: image)
            contentLabel.text = tit
            
            //影藏圆圈
            feedImageView.hidden = true
        }else
        {
            //主页
            setAnimate()
         }
    
    }
   
    




}









